package com.achen.point;

import com.achen.util.RabbitMQUtil;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author achen
 * @create 2020-05-08 13:46
 */
public class Consumer {

    public static void main(String[] args) throws IOException, TimeoutException {
        /*//创建连接mq的连接工厂对象
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //设置连接rabbitmq的主机
        connectionFactory.setHost("192.168.3.36");
        //设置端口号
        connectionFactory.setPort(5672);
        //设置连接哪个虚拟主机（相当于库）
        connectionFactory.setVirtualHost("/ems");
        //设置访问虚拟主机的用户名和密码
        connectionFactory.setUsername("ems");
        connectionFactory.setPassword("123");
        //获取连接对象
        Connection connection = connectionFactory.newConnection();*/

        Connection connection = RabbitMQUtil.getConnection();
        //获取连接中通道
        Channel channel = connection.createChannel();
        //通道绑定消息队列
        channel.queueDeclare("aa",false,false,true,null);
        //消费消息
        channel.basicConsume("aa",true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("=========>" + new String(body));
            }
        });
    }
}
